home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / internet / vbipsmtp / smtp.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-03-09  |  15.9 KB  |  470 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "SMTP Demo"
  4.    ClientHeight    =   6045
  5.    ClientLeft      =   330
  6.    ClientTop       =   375
  7.    ClientWidth     =   9225
  8.    Height          =   6450
  9.    Left            =   270
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   6045
  12.    ScaleWidth      =   9225
  13.    Top             =   30
  14.    Width           =   9345
  15.    Begin VBX.dsSocket dsSocket1 
  16.       BindConnect     =   0   'False
  17.       DataSize        =   2048
  18.       EOLChar         =   0
  19.       Left            =   8055
  20.       LineMode        =   0   'False
  21.       Linger          =   0   'False
  22.       LocalPort       =   0
  23.       RemoteDotAddr   =   ""
  24.       RemoteHost      =   ""
  25.       RemotePort      =   0
  26.       ServiceName     =   ""
  27.       Timeout         =   10
  28.       Top             =   675
  29.    End
  30.    Begin VB.CommandButton btnClear 
  31.       Caption         =   "Clear Messsage"
  32.       Height          =   420
  33.       Left            =   6300
  34.       TabIndex        =   13
  35.       Top             =   855
  36.       Width           =   1500
  37.    End
  38.    Begin VB.CommandButton btnAttach 
  39.       Caption         =   "Attach File"
  40.       Height          =   420
  41.       Left            =   6300
  42.       TabIndex        =   12
  43.       Top             =   1350
  44.       Width           =   1500
  45.    End
  46.    Begin VB.TextBox txtFrom 
  47.       BeginProperty Font 
  48.          name            =   "MS Sans Serif"
  49.          charset         =   1
  50.          weight          =   400
  51.          size            =   9.75
  52.          underline       =   0   'False
  53.          italic          =   0   'False
  54.          strikethrough   =   0   'False
  55.       EndProperty
  56.       Height          =   360
  57.       Left            =   2565
  58.       TabIndex        =   1
  59.       Top             =   675
  60.       Width           =   3525
  61.    End
  62.    Begin VB.TextBox txtSubject 
  63.       BeginProperty Font 
  64.          name            =   "MS Sans Serif"
  65.          charset         =   1
  66.          weight          =   400
  67.          size            =   9.75
  68.          underline       =   0   'False
  69.          italic          =   0   'False
  70.          strikethrough   =   0   'False
  71.       EndProperty
  72.       Height          =   360
  73.       Left            =   2565
  74.       TabIndex        =   3
  75.       Top             =   1395
  76.       Width           =   3525
  77.    End
  78.    Begin VB.TextBox txtTo 
  79.       BeginProperty Font 
  80.          name            =   "MS Sans Serif"
  81.          charset         =   1
  82.          weight          =   400
  83.          size            =   9.75
  84.          underline       =   0   'False
  85.          italic          =   0   'False
  86.          strikethrough   =   0   'False
  87.       EndProperty
  88.       Height          =   360
  89.       Left            =   2565
  90.       TabIndex        =   2
  91.       Top             =   1035
  92.       Width           =   3525
  93.    End
  94.    Begin VB.TextBox txtMsg 
  95.       BeginProperty Font 
  96.          name            =   "MS Sans Serif"
  97.          charset         =   1
  98.          weight          =   400
  99.          size            =   9.75
  100.          underline       =   0   'False
  101.          italic          =   0   'False
  102.          strikethrough   =   0   'False
  103.       EndProperty
  104.       Height          =   3885
  105.       Left            =   180
  106.       MultiLine       =   -1  'True
  107.       ScrollBars      =   3  'Both
  108.       TabIndex        =   4
  109.       Top             =   1935
  110.       Width           =   8925
  111.    End
  112.    Begin VB.CommandButton btnOK 
  113.       Caption         =   "&OK"
  114.       Height          =   375
  115.       Left            =   7965
  116.       TabIndex        =   6
  117.       Top             =   90
  118.       Width           =   1140
  119.    End
  120.    Begin VB.CommandButton btnSend 
  121.       Caption         =   "Send"
  122.       Enabled         =   0   'False
  123.       Height          =   420
  124.       Left            =   7965
  125.       TabIndex        =   5
  126.       Top             =   1350
  127.       Width           =   1140
  128.    End
  129.    Begin VB.TextBox txtHost 
  130.       BeginProperty Font 
  131.          name            =   "MS Sans Serif"
  132.          charset         =   1
  133.          weight          =   400
  134.          size            =   9.75
  135.          underline       =   0   'False
  136.          italic          =   0   'False
  137.          strikethrough   =   0   'False
  138.       EndProperty
  139.       Height          =   360
  140.       Left            =   2565
  141.       TabIndex        =   0
  142.       Top             =   135
  143.       Width           =   3525
  144.    End
  145.    Begin MSComDlg.CommonDialog dlgFile 
  146.       Left            =   8550
  147.       Top             =   630
  148.       _version        =   65536
  149.       _extentx        =   847
  150.       _extenty        =   847
  151.       _stockprops     =   0
  152.    End
  153.    Begin VB.Label Label1 
  154.       Alignment       =   1  'Right Justify
  155.       Caption         =   "Your Address:"
  156.       Height          =   195
  157.       Index           =   5
  158.       Left            =   1035
  159.       TabIndex        =   11
  160.       Top             =   675
  161.       Width           =   1440
  162.    End
  163.    Begin VB.Label Label1 
  164.       Alignment       =   1  'Right Justify
  165.       Caption         =   "Subject:"
  166.       Height          =   195
  167.       Index           =   4
  168.       Left            =   1035
  169.       TabIndex        =   10
  170.       Top             =   1395
  171.       Width           =   1440
  172.    End
  173.    Begin VB.Label Label1 
  174.       Alignment       =   1  'Right Justify
  175.       Caption         =   "Send To:"
  176.       Height          =   195
  177.       Index           =   2
  178.       Left            =   1035
  179.       TabIndex        =   9
  180.       Top             =   1035
  181.       Width           =   1440
  182.    End
  183.    Begin VB.Label Label1 
  184.       AutoSize        =   -1  'True
  185.       Caption         =   "Message:"
  186.       Height          =   195
  187.       Index           =   3
  188.       Left            =   90
  189.       TabIndex        =   8
  190.       Top             =   1710
  191.       Width           =   690
  192.    End
  193.    Begin VB.Label Label1 
  194.       Alignment       =   1  'Right Justify
  195.       Caption         =   "SMTP Server Name:"
  196.       Height          =   195
  197.       Index           =   0
  198.       Left            =   180
  199.       TabIndex        =   7
  200.       Top             =   180
  201.       Width           =   2280
  202.    End
  203. Attribute VB_Name = "Form1"
  204. Attribute VB_Creatable = False
  205. Attribute VB_Exposed = False
  206. Option Explicit
  207. '---------------------------------------------------
  208. 'SMTP.FRM
  209. 'Copyright 1996 by Carl Franklin
  210. 'Unauthorized reproduction in any medium of this
  211. 'source code is strictly prohibited without written
  212. 'permission from the author and John Wiley & Sons.
  213. '---------------------------------------------------
  214. Sub CheckFields()
  215.     '-- Enables the send button only
  216.     '   if all the fields are filled in.
  217.     If Len(txtHost) Then
  218.         If Len(txtFrom) Then
  219.             If Len(txtTo) Then
  220.                 If Len(txtSubject) Then
  221.                     If Len(txtMsg) Then
  222.                         btnSend.Enabled = True
  223.                         Exit Sub
  224.                     End If
  225.                 End If
  226.             End If
  227.         End If
  228.     End If
  229.     btnSend.Enabled = False
  230. End Sub
  231. Private Sub btnAttach_Click()
  232.     '-- Set File Dialog options
  233.     dlgFile.Filter = "All Files|*.*"
  234.     dlgFile.DialogTitle = "Select File To Attach"
  235.     dlgFile.filename = "*.*"
  236.     dlgFile.CancelError = True
  237.     '-- Pop up the file dialog box
  238.     On Error Resume Next
  239.     dlgFile.Action = 1
  240.     If Err Then
  241.         '-- Cancel was pressed
  242.         Exit Sub
  243.     End If
  244.     '-- Add this file to the attached file array
  245.     If Len(dlgFile.filename) Then
  246.         gnNumAttachedFiles = gnNumAttachedFiles + 1
  247.         ReDim Preserve gszAttachedFiles(1 To gnNumAttachedFiles) As String
  248.         gszAttachedFiles(gnNumAttachedFiles) = dlgFile.filename
  249.         '-- Print the file name in the Message text box
  250.         txtMsg.SelStart = Len(txtMsg) + 1
  251.         txtMsg.SelText = vbCRLF & "[[ATTACHMENT: " & UCase$(dlgFile.FileTitle) & "]]"
  252.     End If
  253. End Sub
  254. Private Sub btnClear_Click()
  255.     '-- Clear the email message
  256.     txtMsg = ""
  257.     gnNumAttachedFiles = 0
  258. End Sub
  259. Private Sub btnOK_Click()
  260.     '-- Outta here
  261.     Unload Me
  262. End Sub
  263. Sub btnSend_Click()
  264. '-- Send an Email message. All we have to do here is
  265. '   fill in the global strings that make up the message
  266. '   from the text controls, and connect. The protocol does
  267. '   the rest.
  268.         
  269.     Dim nErrCode    As Integer
  270.     Dim nPos        As Integer
  271.     Dim nPos2       As Integer
  272.     Dim szMsg       As String
  273.     '-- Disable the buttons
  274.     btnSend.Enabled = False
  275.     btnOK.Enabled = False
  276.     Screen.MousePointer = vbHourglass
  277.     '-- Fill in the global strings
  278.     gszFrom = txtFrom
  279.     gszTo = txtTo
  280.     gszSubject = txtSubject
  281.     gszMsg = txtMsg
  282.     '-- Do we have any attached files here?
  283.     If gnNumAttachedFiles Then
  284.         '-- Remove [[ATTACHMENT ...]] lines
  285.         szMsg = txtMsg
  286.         Do
  287.             nPos = InStr(szMsg, "[[ATTACHMENT")
  288.             If nPos Then
  289.                 nPos2 = InStr(Mid$(szMsg, nPos), vbCRLF)
  290.                 If nPos2 Then
  291.                     szMsg = Left$(szMsg, nPos - 1) & Mid$(szMsg, nPos + nPos2 + 1)
  292.                 Else
  293.                     szMsg = Left$(szMsg, nPos - 1)
  294.                 End If
  295.             Else
  296.                 Exit Do
  297.             End If
  298.         Loop
  299.         
  300.         '-- UUEncode the attached files (requires UUCODE.BAS)
  301.         nErrCode = nMakeMsgWithFiles(szMsg, gszAttachedFiles(), gszAttachFile)
  302.         
  303.         If nErrCode Then
  304.             '-- An error occurred.
  305.             btnSend.Enabled = True
  306.             btnOK.Enabled = True
  307.             Screen.MousePointer = vbNormal
  308.             MsgBox "Error when attaching files: " & Error$(nErrCode), vbExclamation
  309.             Exit Sub
  310.         End If
  311.     End If
  312.     '-- Connect to the host
  313.     If SocketConnect(DSSocket1, 25, (txtHost), 30) Then
  314.         '-- An error occurred.
  315.         btnSend.Enabled = True
  316.         btnOK.Enabled = True
  317.         Screen.MousePointer = vbNormal
  318.         MsgBox "Could not connect", vbInformation, "SMTP Client"
  319.     End If
  320.     '-- The protocol takes over from here (DSSocket1_Receive)
  321. End Sub
  322. Sub DSSocket1_Close(ErrorCode As Integer, ErrorDesc As String)
  323.     gnConnected = False
  324. End Sub
  325. Private Sub DSSocket1_Connect()
  326.     gnConnected = True
  327. End Sub
  328. Sub DSSocket1_Receive(ReceiveData As String)
  329. '-- SMTP Client Protocol in action!
  330.     Dim nPos        As Integer      '-- Used with Instr
  331.     Dim nErrCode    As Integer
  332.     Dim nIndex      As Integer
  333.     Dim szFullMsg   As String
  334.     Dim chrTab      As String
  335.     Const chrSpace = " "            '-- Used with Instr
  336.     Const szPeriod = "."            '-- Used to determine the end of x-mission.
  337.     Static nTextMode As Integer     '-- When True, we are receiving data
  338.                                     '   When False, reply codes.
  339.     Static nCode As Integer         '-- The last reply code received.
  340.     Static bReceived220 As Integer  '-- Set true after receiving the first
  341.                                     '   220, indicating a connection.
  342.     chrTab = Chr$(9)
  343. '------------------------------------------------------------------------------
  344.     '-- Grab the reply code.
  345.     nCode = Val(Left$(ReceiveData, 3))
  346.     '-- What is it?
  347.     Select Case nCode
  348.         
  349.         Case 220    '-- Connect and/or Command OK.
  350.             '-- Is this the first 220?
  351.             If Not bReceived220 Then
  352.                 '-- Yep. Flip the flag.
  353.                 bReceived220 = True
  354.                 
  355.                 '-- This means we're connected. At this
  356.                 '   point SocketConnect will exit.
  357.                 gnConnected = True
  358.                 
  359.                 '-- Send the MAIL command to initiate the send
  360.                 '   process.
  361.                 SendSMTPCommand DSSocket1, "MAIL FROM: <" & gszFrom & ">"
  362.             End If
  363.         
  364.         Case 250, 251    '-- Command OK
  365.             '-- What was the last command?
  366.             Select Case gszCommand
  367.                 Case "MAIL"
  368.                     '-- After MAIL, send the RCPT command to
  369.                     '   establish the final destination
  370.                     SendSMTPCommand DSSocket1, "RCPT TO: <" & gszTo & ">"
  371.                 Case "RCPT"
  372.                     '-- After RCPT, send the DATA command
  373.                     '   to request permission to send the mail message.
  374.                     '   This should yield a 354 reply.
  375.                     SendSMTPCommand DSSocket1, "DATA"
  376.                 Case "DATA"
  377.                     '-- We have just sent the message successfully.
  378.                     
  379.                     '-- Confirmation that the message was delivered.
  380.                     MsgBox "Message Delivered", vbInformation, "SMTP Client"
  381.                     
  382.                     btnSend.Enabled = True
  383.                     btnOK.Enabled = True
  384.                     Screen.MousePointer = vbNormal
  385.                     bReceived220 = False
  386.                 Case "VRFY"
  387.                     MsgBox Mid$(ReceiveData, 4), vbInformation, "User Verified"
  388.             End Select
  389.         
  390.         Case 354
  391.             '-- There should only be one command... DATA
  392.             Select Case gszCommand
  393.                 Case "DATA"
  394.                     '-- Now we have permission to send the message.
  395.                     '   Compose the complete message. Note the date format.
  396.                     '   This is very important.
  397.                     If gnNumAttachedFiles Then
  398.                         nErrCode = nSendFileAsMsg(gszAttachFile, DSSocket1, 8192, gszFrom, gszTo, gszSubject, "")
  399.                     Else
  400.                         szFullMsg = "DATE: " & Format$(Now, "dd mmm yy ttttt") & vbCRLF _
  401.                            & "FROM: " & gszFrom & vbCRLF _
  402.                            & "TO: " & gszTo & vbCRLF _
  403.                            & "SUBJECT: " & gszSubject & vbCRLF & vbCRLF _
  404.                            & gszMsg & vbCRLF & "." & vbCRLF
  405.                         '-- Don't use SendSMTP command, so the
  406.                         '   last command will still be "DATA"
  407.                         SendData DSSocket1, szFullMsg
  408.                     End If
  409.             End Select
  410.         
  411.         Case 551
  412.             '-- What was the last command?
  413.             Select Case gszCommand
  414.                 Case "RCPT"
  415.                     '-- The specified recipient does not exist here but there is
  416.                     '   a forwarding address. Parse it and resend the RCPT command
  417.                     gszTo = szParseString(ReceiveData, "<", 2)
  418.                     nPos = InStr(gszTo, ">")
  419.                     If nPos Then
  420.                         gszTo = Left$(gszTo, nPos - 1)
  421.                         SendSMTPCommand DSSocket1, "RCPT TO: <" & gszTo & ">"
  422.                     End If
  423.             End Select
  424.             
  425.         Case Is >= 400
  426.             
  427.             '-- An error of some sort occurred. Display to the user and
  428.             '   reset everything.
  429.             
  430.             MsgBox Mid$(ReceiveData, 4), vbInformation, "Error From Server"
  431.             btnSend.Enabled = True
  432.             btnOK.Enabled = True
  433.             Screen.MousePointer = vbNormal
  434.             bReceived220 = False
  435.             
  436.         Case Else
  437.             '-- Something we were'nt expecting
  438.             Debug.Print ReceiveData
  439.     End Select
  440. End Sub
  441. Sub DSSocket1_SendReady()
  442.     gnSendReady = True
  443. End Sub
  444. Sub Form_Load()
  445.     DSSocket1.LineMode = True
  446. End Sub
  447. Sub Form_Unload(Cancel As Integer)
  448.     SocketDisconnect DSSocket1
  449.     End
  450. End Sub
  451. Private Sub txtFrom_Change()
  452.     CheckFields
  453. End Sub
  454. Private Sub txtHost_Change()
  455.     CheckFields
  456. End Sub
  457. Private Sub txtMsg_Change()
  458.         
  459.     CheckFields
  460.     If Len(txtMsg) = 0 Then
  461.         gnNumAttachedFiles = 0
  462.     End If
  463. End Sub
  464. Private Sub txtSubject_Change()
  465.     CheckFields
  466. End Sub
  467. Private Sub txtTo_Change()
  468.     CheckFields
  469. End Sub
  470.